package com.spark.sort.select;

/**
 * @Author sc
 * @DATE 2024/6/6 1:41
 * @Description:
 * @Version 1.0
 */
public class SelectSort {
    public static void main(String[] args) {

        Integer[] arr = {1,3,4,2,5,1,2,4,55,77,65,22,21313,131535,3131,45464,132535,22223,41,-1};
        for (Integer integer : arr) {
            System.out.print(integer + " ");
        }
        System.out.println("\n===============选择排序开始=================");
        selectSort(arr);
        for (Integer integer : arr) {
            System.out.print(integer + " ");
        }

    }

    public static void selectSort(Integer[] arr){
        for (int i = 0; i < arr.length - 1; i++) { // 选择排序的次数
            int min = arr[i];
            int minIndex = i;
            for(int j = i + 1;j < arr.length;j++){
                if (min > arr[j]){
                    min = arr[j];
                    minIndex = j;
                }

                if (i != minIndex){
                    arr[minIndex] = arr[i];
                    arr[i] = min;
                }

            }
        }


    }
}
